package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClientClaim;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientClaimService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("clientClaim")
public class ClientClaimController {

    @Autowired
    IClientClaimService clientClaimService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"clientClaim:view","客户赔损明细页面"},logical = Logical.OR)
    public String view() {
        return "clientClaim";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"clientClaim:query","客户赔损明细列表"},logical = Logical.OR)
    public PageResult query(QueryObject qo) {
        return clientClaimService.query(qo);
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, EmployeeQueryObject qo) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");
        //创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("sheet01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("报案人");
        row.createCell(1).setCellValue("联系电话");
        row.createCell(2).setCellValue("保单编号");
        row.createCell(3).setCellValue("车牌号码");
        row.createCell(4).setCellValue("事故详情");
        row.createCell(5).setCellValue("理赔收款人");
        row.createCell(6).setCellValue("理赔金额");
        row.createCell(7).setCellValue("拨款卡号");
        row.createCell(8).setCellValue("拨款时间");
        List<ClientClaim> list = clientClaimService.selectByKeyword(qo);
        for (int i = 0; i < list.size(); i++) {
            ClientClaim clientClaim = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(clientClaim.getClaim().getReporter());
            row.createCell(1).setCellValue(clientClaim.getClaim().getTel());
            row.createCell(2).setCellValue(clientClaim.getClaim().getStaging());
            row.createCell(3).setCellValue(clientClaim.getClaimitem().getCarnumber());
            row.createCell(4).setCellValue(clientClaim.getClaimitem().getParticulars());
            row.createCell(5).setCellValue(clientClaim.getDamage().getPayee());
            row.createCell(6).setCellValue(clientClaim.getDamage().getMoney());
            row.createCell(7).setCellValue(clientClaim.getDamage().getCarnumber());
            CreationHelper createHelper = workbook.getCreationHelper();
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(
                    createHelper.createDataFormat().getFormat("m/d/yy"));
            HSSFCell cellDate = row.createCell(8);
            cellDate.setCellStyle(cellStyle);
            cellDate.setCellValue(clientClaim.getDamage().getDisposeTime());
        }
        workbook.write(response.getOutputStream());
    }
}
